<?php
/**
 * 公用函数库
 *
 */
class Helper_Common {
	
	
	
	static function compresshtml($str)
	{
		$aa=preg_replace("/(^http:)*\/\/[\S^;]*;/","",$str);
		$aa=preg_replace("/\<\!\-\-[\s\S]*?\-\-\>/","",$aa);
		$aa=preg_replace("/\>[\s]+\</","><",$aa);
		$aa=preg_replace("/;[\s]+/",";",$aa);
		$aa=preg_replace("/[\s]+\}/","}",$aa);
		$aa=preg_replace("/}[\s]+/","}",$aa);
		$aa=preg_replace("/\{[\s]+/","{",$aa);
		$aa=preg_replace("/([\s]){2,}/","$1",$aa);
		$aa=preg_replace("/[\s]+\=[\s]+/","=",$aa);
		$aa=preg_replace("/[\s]+\=[\s]+/","=",$aa);
		return $aa;
	}
	/**
	 * 
	 *2011-12-30-下午05:30:52 by 460932465
	 * 返回格式化后的字符串
	 * @param 左边字符 $leftchar
	 * @param 右边字符 $rightchar
	 * @param 完整字符串 $string
	 */
	static function get_split($leftchar,$rightchar,$string)
	{
		$a =  strpos($string,$leftchar) ;
		$b = strpos($string,$rightchar);
		if($a === false && $b === false)
		{
			return false;
		}else
		{
			$c = substr($string,$a + strlen($leftchar),$b - $a - strlen($leftchar));
			$c = str_replace("\n","",$c);
			$c = str_replace("\r","",$c);
			$c = str_replace("\r\n","",$c);
			return $c;
		}
	}
	
	
	static function get_object_vars_final($obj){
		if(is_object($obj)){
			$obj = get_object_vars($obj);
		}
		if(is_array($obj)){
			foreach ($obj as $key=>$value){
				$obj[$key] = self::get_object_vars_final($value);
			}
		}
		return $obj;
	}
	//解析xml方法
	static function get_xml_data ($strXml) {
		$pos = strpos($strXml, 'xml');
		if ($pos) {
			$xmlCode = simplexml_load_string($strXml,'SimpleXMLElement', LIBXML_NOCDATA);
			$arrayCode= self:: get_object_vars_final($xmlCode);
			return $arrayCode ;
		} else {
			return false;
		}
	}
/*
	 *访问URL
	 */
	static function curl($url, $postFields = null)
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_FAILONERROR, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		if (is_array($postFields) && 0 < count($postFields))
		{
			$postBodyString = "";
			$postMultipart = false;
			foreach ($postFields as $k => $v)
			{
				if("@" != substr($v, 0, 1))//判断是不是文件上传
				{
					$postBodyString .= "$k=" . urlencode($v) . "&"; 
				}
				else//文件上传用multipart/form-data，否则用www-form-urlencoded
				{
					$postMultipart = true;
				}
			}
			unset($k, $v);
			curl_setopt($ch, CURLOPT_POST, true);
			if ($postMultipart)
			{
				curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
			}
			else
			{
				curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
			}
		}
		$reponse = curl_exec($ch);
		
		if (curl_errno($ch))
		{
			throw new Exception(curl_error($ch),0);
		}
		else
		{
			$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
			if (200 !== $httpStatusCode)
			{
				throw new Exception($reponse,$httpStatusCode);
			}
		}
		curl_close($ch);
		return $reponse;
	}
	
	
	/**
	 * 系统加密算法
	 *
	 * @param 加密原串 $var
	 * @return string
	 */
	public static function system_md5($var)
	{
		$_md5 = md5($var);
		$b = array();
		for ($i=0 ; $i<32 ; $i+=2)
		{
			if (fmod($i,3)){
				$b[] = strrev(substr($_md5,$i,2));
			}else{
				$b[] = substr($_md5,$i,2);
			}
		}
		return md5(implode('',$b));
	}
	/**
	 * 
	 * 获取毫秒数
	 */
	public static function microtime_float()
	{
		list($usec, $sec) = explode(' ', microtime());
		return $sec * 1000  + intval($usec * 1000);
	}
	/**
	 * 获得用户的真实IP地址
	 *
	 * @access  public
	 * @return  string
	 */
	static function clientip(){
		static $realip = NULL;

		if ($realip !== NULL)
		{
			return $realip;
		}

		if (isset($_SERVER))
		{
			if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
			{
				$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);

				/* 取X-Forwarded-For中第一个非unknown的有效IP字符串 */
				foreach ($arr AS $ip)
				{
					$ip = trim($ip);

					if ($ip != 'unknown')
					{
						$realip = $ip;

						break;
					}
				}
			}
			elseif (isset($_SERVER['HTTP_CLIENT_IP']))
			{
				$realip = $_SERVER['HTTP_CLIENT_IP'];
			}
			else
			{
				if (isset($_SERVER['REMOTE_ADDR']))
				{
					$realip = $_SERVER['REMOTE_ADDR'];
				}
				else
				{
					$realip = '0.0.0.0';
				}
			}
		}
		else
		{
			if (getenv('HTTP_X_FORWARDED_FOR'))
			{
				$realip = getenv('HTTP_X_FORWARDED_FOR');
			}
			elseif (getenv('HTTP_CLIENT_IP'))
			{
				$realip = getenv('HTTP_CLIENT_IP');
			}
			else
			{
				$realip = getenv('REMOTE_ADDR');
			}
		}

		preg_match("/[\d\.]{7,15}/", $realip, $onlineip);
		$realip = !empty($onlineip[0]) ? $onlineip[0] : '127.0.0.8';

		return $realip;
	}
	/**
	 * 
	 *2011-12-24-上午11:18:22 by 460932465
	 * @access 无限分级格式化
	 * @param 无限数组 $array
	 * @param 模板 $template
	 * @param 子模板
	 */
	static function format_array($array,$template,$subtemplate)
	{
		$result = '';
		foreach ($array as $k=>$v)
		{
			$line = '';
			if (is_array($v))
			{
				$line = str_replace('{$key}', $k, $subtemplate);
				$array_content = self::format_array($v, $template,$subtemplate);
				$line = str_replace('{$value}', $array_content, $line);
			}else
			{
				$line = str_replace('{$key}', $k, $template);
				$line = str_replace('{$value}', $v, $line);
			}
		$result .= $line;
		}
		return $result;
	}
}